package com.block.api.controller.member;

import com.alibaba.fastjson.JSONObject;
import com.block.common.annotate.NeedToken;
import com.block.common.bean.CommonParam;
import com.block.common.bean.SessionBean;
import com.block.common.exception.ApiException;
import com.block.common.model.dto.LoginReqDTO;
import com.block.common.security.util.ApiUtil;
import com.block.common.vo.ResponseVo;
import com.block.db.entity.AppEncrypt;
import com.block.db.entity.ApplicationInfo;
import com.block.db.entity.ChannelBaseInfo;
import com.market.member.service.IMemberBaseInfoService;
import com.market.member.service.IMemberLoginSessionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("api/member")
@Validated
public class MemberController {

    @Autowired
    IMemberBaseInfoService memberBaseInfoService;
    @Autowired
    IMemberLoginSessionService memberLoginSessionService;

    /**
     * 用户登陆
     */
    @RequestMapping(value = "/login", method = {RequestMethod.POST, RequestMethod.GET})
    public String login(@RequestAttribute(value = "tranData") JSONObject tranData,
                             @RequestAttribute(value = "appEncrypt") AppEncrypt appEncrypt,
                             @RequestAttribute(value = "uri") String uri,
                             @RequestAttribute(value = "commonParam") CommonParam commonParam,
                             @RequestAttribute(value = "startDate") Date startdate,
                             @RequestAttribute(value = "ip") String ip) throws ApiException {

        String loginType = tranData.getString("loginType");
        String mobile = tranData.getString("mobile");
        String loginCode = tranData.getString("loginCode");// 短信验证码/一键登录token

        ChannelBaseInfo channelBaseInfo = commonParam.getChannelBaseInfo();
        ApplicationInfo applicationInfo = commonParam.getApplicationInfo();

        LoginReqDTO loginReqDTO = new LoginReqDTO();
        loginReqDTO.setLoginType(loginType);
        loginReqDTO.setMobile(mobile);
        loginReqDTO.setLoginCode(loginCode);
        loginReqDTO.setReqIp(ip);
        loginReqDTO.setChannelCode(channelBaseInfo.getChannelCode());
        loginReqDTO.setChannelName(channelBaseInfo.getChannelName());
        loginReqDTO.setAppCode(applicationInfo.getAppCode());

        ResponseVo responseVo = memberBaseInfoService.toLogin(loginReqDTO, commonParam);

        return ApiUtil.dealReturnJson(responseVo, appEncrypt,  startdate, uri);
    }

    /**
     * 用户推出登陆
     */
    @NeedToken
    @RequestMapping(value = "/logout", method = {RequestMethod.POST, RequestMethod.GET})
    public String logout(@RequestAttribute(value = "tranData") JSONObject tranData,
                              @RequestAttribute(value = "appEncrypt") AppEncrypt appEncrypt,
                              @RequestAttribute(value = "uri") String uri,
                              @RequestAttribute(value = "sessionBean") SessionBean sessionBean,
                              @RequestAttribute(value = "commonParam") CommonParam commonParam,
                              @RequestAttribute(value = "startDate") Date startdate,
                              @RequestAttribute(value = "ip") String ip) throws ApiException {

        ResponseVo responseVo = memberLoginSessionService.logout(sessionBean);
        return ApiUtil.dealReturnJson(responseVo, appEncrypt,  startdate, uri);
    }

    /**
     * 用户登陆
     *
     */
    @NeedToken
    @RequestMapping(value = "/getUserInfo", method = {RequestMethod.POST, RequestMethod.GET})
    public String getUserInfo(@RequestAttribute(value = "tranData") JSONObject tranData,
                        @RequestAttribute(value = "appEncrypt") AppEncrypt appEncrypt,
                        @RequestAttribute(value = "uri") String uri,
                        @RequestAttribute(value = "sessionBean") SessionBean sessionBean,
                        @RequestAttribute(value = "commonParam") CommonParam commonParam,
                        @RequestAttribute(value = "startDate") Date startdate,
                        @RequestAttribute(value = "ip") String ip) throws ApiException {

        ResponseVo responseVo = memberBaseInfoService.getUserInfo(sessionBean, commonParam);
        return ApiUtil.dealReturnJson(responseVo, appEncrypt,  startdate, uri);
    }


}
